<?php
/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2018/7/31 0031
 * Time: 上午 10:13
 */
namespace app\admin\controller;
use app\common\controller\Curt;
use think\Db;
use think\Request;
class Goodsclassify extends Curt{
    public function __construct()
    {
        parent::__construct($upload = true,$uploadConfig = array(
            'input_name' => 'coverFile',//文本框name
            'data_field' => 'classify_image'//数据库字段
        ));
    }

    public function getall()
    {
        $keyword=input('get.keyword');
        $where['father_id']=0;
        if($keyword){
            $where['classify_name']=['like','%'.$keyword.'%'];//模糊查询or可以一个条件中写多个字段以‘|’分开
        }
        $list = $this->model
            ->order('sort asc,id asc')
            ->where($where)
            ->paginate();
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
    }

    public function showeradd()
    {
        $id=input("id");
        /*查询分类名*/
        $data=Db::name('goodsclassify')->where("id",$id)->find();
        $this->assign('data',$data);
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function showeredit()
    {
        $id=input("id");
        /*查询分类*/
        $data=Db::name('goodsclassify')->where("id",$id)->find();
        //查询父类名称
        $datas=Db::name('goodsclassify')->where("id",$data['father_id'])->find();
        $this->assign('data',$data);
        $this->assign('datas',$datas);
        $this->assign('id',$id);
        return $this->fetch();
    }
    //二级分类列表
    public function showerlist(){
        $keyword=input('get.keyword');
        $where['a.father_id']=input('id');
        if($keyword){
            $where['a.classify_name']=['like','%'.$keyword.'%'];//模糊查询or可以一个条件中写多个字段以‘|’分开
        }
        $list = Db::name('goodsclassify')
            ->alias('a')
            ->join('ec_goodsclassify b','b.id=a.father_id',left)
            ->order('id desc')
            ->field('b.classify_name as names,b.id as ids,a.*')
            ->where($where)
            //->fetchSql(true)
            ->paginate();
        //dump($list);die;
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('names', $list[0]['names']);
        $this->assign('father_id', input('id'));
        $this->assign('page', $page);
        return $this->fetch();
    }
    //添加二级分类
    public function adds(){
        $data=$this->request->param();
        $validate=validate($this->table);
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        if($this->upload){
            $file = request()->file($this->uploadsconfig['input_name']);
            if($file){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size'=>$this->uploads['SIZE'],'ext'=>$this->uploads['EXT']])->move($this->uploads['PATH']);
                if($info){
                    $data[$this->uploadsconfig['data_field']]= date("Ymd").'/'.$info->getFilename();
                }else{
                    $this->error($file->getError());
                }
            }
        }
        $data=$this->adddatabefter($data);
        $res=$this->model->allowField(true)->save($data);
        if($res){
            $this->success('添加成功',url('showerlist',array('id'=>$data['father_id'])));
        }else{
            $this->error('添加失败',url('showerlist',array('id'=>$data['father_id'])));
        }
    }
    //删除一级分类
    public function del_first(){
        $id=input("id");
        //查询是否有下级分类
        $erji_count=Db::name("goodsclassify")->where('father_id',$id)->count();
        if($erji_count>0){
            $this->success('此分类下有二级分类禁止删除',url('showlist'));
        }else{
            $del=Db::name('goodsclassify')->where('id',$id)->delete();
            if($del){
                $this->success('删除成功',url('showlist'));
            }
        }

    }

    //删除二级分类
    public function del_two(){
        $id=input("id");
        //查询分类下是否有商品
        $goods_count=Db::name("goods")->where('classify_id',$id)->count();
        //查询父类id
        $father_id=Db::name("goodsclassify")->where('id',$id)->find();
        if($goods_count>0){
            $this->success('此分类下有商品禁止删除',url('showerlist',array('id'=>$father_id['father_id'])));
        }else{
            $del=Db::name('goodsclassify')->where('id',$id)->delete();
            if($del){
                $this->success('删除成功',url('showerlist',array('id'=>$father_id['father_id'])));
            }
        }
    }
    //编辑二级分类
    public function edits()
    {
        $data=$this->request->param();//获取值
        $validate=validate($this->table);//验证器验证
        //数据自动验证
        if(!$validate->scene('edit')->check($data)){
            $this->error($validate->getError());
        }
        $file = request()->file($this->uploadsconfig['input_name']);
        if($file){
            $info = $this->model->find($data['id']);
            $info =$info->toArray();
            if($info['cover']){
//                $url = $this->uploads['PATH'].'/'.$info['cover'];
//                unlink($url);
             }
            $info = $file->validate(['size'=>$this->uploads['SIZE'],'ext'=>$this->uploads['EXT']])->move($this->uploads['PATH']);
            if($info){
                if ($type){
                    $this->uploadsconfig['data_field'] = 'goods_image';
                }
                $data[$this->uploadsconfig['data_field']]= date("Ymd").'/'.$info->getFilename();
            }else{
                $this->error($file->getError());
            }
        }
        $data = $this->editAfter($data);
        $info=$this->model->allowField(true)->isUpdate(true) ->save($data);
        if ($info) {
            $this->success("编辑成功",url('showerlist',array('id'=>$data['father_id'])));
        } else {
            $this->success("未做修改",url('showerlist',array('id'=>$data['father_id'])));
        }
    }
    /*
    * 分类在首页是否显示操作
    */
    public function isshow()
    {
        /*查询分类显示有没有8个*/
        $count=Db::name("goodsclassify")->where('father_id',0)->where('is_show',1)->count();
        if($count<8) {
            $status = Request::instance()->param('is_show');//取值同input
            $where['id'] = Request::instance()->param('id');
            $station = $status == 1 ? 2 : 1;
            if ($where['id']) {
                $res = $this->model->where($where)->setField('is_show', $station);
                if ($res) {
                    $this->success('操作成功', $this->fetchconfig['add_success']);
                } else {
                    $this->error('网络繁忙，请稍后重试', $this->fetchconfig['add_success']);
                }
            }
        }else{
            $this->error('首页分类显示已满，不能再显示多余分类', $this->fetchconfig['add_success']);
        }
    }

}